package flc.ast.view;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.guesstv.lib.module.GtDataProvider;
import com.stark.guesstv.lib.module.GtOptionGenerator;
import com.stark.guesstv.lib.module.bean.GuessOption;
import com.stark.guesstv.lib.module.bean.TvActorBean;
import com.stark.guesstv.lib.module.constant.GuessType;
import com.stark.guesstv.lib.module.util.GtPrefUtil;
import com.stark.guesstv.lib.ui.GuessImgView;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import stark.common.basic.base.APresenter;

public class GuessPicPresenter extends APresenter<GuessImgView> {

    //答案选项数量
    private static final int OPTION_COUNT = 28;

    private GuessType mGuessType;
    private TvActorBean mShowActorBean;

    public GuessPicPresenter(@NonNull GuessType guessType, @NonNull TvActorBean actorBean) {
        mGuessType = guessType;
        mShowActorBean = actorBean;
    }

    public GuessType getGuessType() {
        return mGuessType;
    }

    /**
     * 显示当前的猜电视剧或演员项
     */
    public void showCurTvActor() {
        List<String> options = GtOptionGenerator.generateOptions(mGuessType, mShowActorBean, OPTION_COUNT);
        List<GuessOption> guessOptions = new ArrayList<>();
        if (options != null) {
            GuessOption guessOption;
            for (String option : options) {
                guessOption = new GuessOption();
                guessOption.content = option;
                guessOptions.add(guessOption);
            }
        }
        if (isViewAttached()) {
            view.onShowCurTvActor(mShowActorBean, guessOptions);
        }
    }

    /**
     * 下一个
     */
    public void next(boolean flag) {
        List<TvActorBean> beans;
        int curPass = 0;
        if (mGuessType == GuessType.TV) {
            beans = GtDataProvider.getTvData();
            curPass = GtPrefUtil.getTvPass();
        } else {
            beans = GtDataProvider.getActorData();
            curPass = GtPrefUtil.getActorPass();
        }
        int index = beans.indexOf(mShowActorBean);
        if (index == beans.size() - 1) {
            //完成了所有关卡
            if (isViewAttached()) {
                view.onCompleteAllPass();
            }
        } else {
            if (flag) {
                if (mShowActorBean.id + 1 > curPass) {
                    ToastUtils.showShort(R.string.gt_cur_pass_no_lock);
                    return;
                }
            }
            mShowActorBean = beans.get(flag ? index + 1 : index - 1);
            showCurTvActor();
        }
    }

    /**
     * 检测答案是否正确
     *
     * @param answer
     * @return true：正确
     */
    public void checkAnswer(@NonNull String answer) {
        boolean isRight = answer.equals(mShowActorBean.name);
        if (isRight) {
            //答案正确，解锁下一关
            int curPass = 0;
            if (mGuessType == GuessType.TV) {
                curPass = GtPrefUtil.getTvPass();
                if (mShowActorBean.id + 1 > curPass) {
                    GtPrefUtil.saveTvPass(mShowActorBean.id + 1);
                }
            } else {
                curPass = GtPrefUtil.getActorPass();
                if (mShowActorBean.id + 1 > curPass) {
                    GtPrefUtil.saveActorPass(mShowActorBean.id + 1);
                }
            }
        }
        if (isViewAttached()) {
            view.onCheckAnswerRet(isRight);
        }
    }
}
